package org.jground.zk.lab;

import java.io.IOException;

import org.jground.rx.ui.zk.UIStyles;
import org.jground.util.ArrayUtils;
import org.jground.zk.zulX.WindowX;
import org.zkoss.image.AImage;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Html;
import org.zkoss.zul.Image;
import org.zkoss.zul.Popup;

public class ImageView extends WindowX implements EventListener {

    private Popup popup = new Popup();
    private Image displayImage = new Image();
    private Image fullScreenIcon = new Image();

    public ImageView(Page page) {
        this.setPage(page);
        //
        fullScreenIcon.setContent(UIStyles.getInstance().getAImage(UIStyles.ImageKey.fullscreen16));
        fullScreenIcon.addEventListener(Events.ON_CLICK, this);
        add(fullScreenIcon);
        //
        add(popup);
        try {
            displayImage.setContent(new AImage(
                    "/tab/person/daniela/album2009/20090127/2009_01_27__19_32-dscn0638-2009_01_27__19_32.jpg"));
            displayImage.setParent(popup);
            displayImage.addEventListener(Events.ON_CLICK, this);
        } catch (IOException e) {
            Html html = new Html("Could not open image! " + ArrayUtils.join(e.getStackTrace()));
            html.setParent(popup);
        }

    }

    public void onEvent(Event event) throws Exception {
        if (event.getTarget() == displayImage) {
            popup.close();
        } else if (event.getTarget() == fullScreenIcon) {
            popup.open(fullScreenIcon);
        }
    }

}
